<?php
session_start();
isLogin();

if(isset($_POST["Submits"])){
	checkNotNull(array('name'),"?".$_SERVER['QUERY_STRING'],"栏目相关");
	if($_REQUEST['act']=='modify'){
		$sql="update ".$prefix."column set name='".$_POST['name']."',c_show='".$_POST['c_show']."',type='".$_POST['type']."',pid=".$_POST['pid'].",moban='".$_POST['moban']."',folder='".$_POST['folder']."',paixu='".$_POST['paixu']."',title='".$_POST['title']."',keywords='".$_POST['keywords']."',description='".$_POST['description']."',link='".$_POST['link']."' where id=".$_REQUEST['id'];
		$pre="修改";
	}else{
		$sql="insert into ".$prefix."column (name,c_show,type,pid,folder,paixu,title,keywords,description,moban,link) values('".$_POST['name']."',".$_POST['c_show'].",'".$_POST['type']."',".$_POST['pid'].",'".$_POST['folder']."','".$_POST['paixu']."','".$_POST['title']."','".$_POST['keywords']."','".$_POST['description']."','".$_POST['moban']."','".$_POST['link']."')";
		$pre="添加";
	}
	if(mysql_query($sql)){
		reload_js($pre."成功",$_POST['fromurl']);
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}

if(isset($_POST['customSubmit'])){
	$sql="update ".$prefix."column set content='".$_POST['content']."' where id=".$_REQUEST['catalog'];
	$pre="栏目内容修改";
	if(mysql_query($sql)){
		reload_js($pre."成功",$_POST['fromurl']);
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}

$arr="";
if($_REQUEST['act']=='modify'){
	$arr=get_list_by_id('column','id',$_REQUEST['id']);
	if(!$arr){
		reload_js("错误的参数id","?cmd=column");
	}
}elseif($_REQUEST['act']=='del'){
	$sql="delete from ".$prefix."column where id=".$_GET['id'];
	$pre="删除";
	if(mysql_query($sql)){
		reload_js($pre."成功",$_SERVER['HTTP_REFERER']);
	}else{
		reload_js($pre."失败，原因：程序错误，请联系技术人员进行修改","index.php");
	}
}elseif($_REQUEST['act']=='custom'){
	$arr=get_field_by_id('column','content,name','id',$_REQUEST['catalog']);
	if(!$arr){
		reload_js("错误的参数id","?cmd=column");
	}
}

function get_catalog_list_by_id($id,$ji){
	global $prefix,$thiscat;
	
	$result=mysql_query("select * from ".$prefix."column where pid=".$id." order by paixu ");
	$pre='';
	if($_GET['act']=='add'){
		$pid=$_GET['id'];
	}else{
		$pid=get_field_by_id('column','pid','id',$_GET['id']);
		$pid=$pid['pid'];
	}
	for($i=0;$i<$ji;$i++){$pre .= "--";}
	while($arr=mysql_fetch_array($result)){
		if($arr['id']==$pid){
			$s="selected";	
		}else{
			$s='';
		}
		if($arr['type']=='group'){
			echo "<option value='".$arr['id']."' ".$s.">".$pre.$arr['name']."</option>";
		}
		$a=mysql_query("select * from ".$prefix."column where pid=".$arr['id']." order by paixu ");	
		if($b=mysql_fetch_array($a)){
			$ji++;
			get_catalog_list_by_id($arr['id'],$ji);
			$ji=0;
		}
	}
}

?>
<script type="text/javascript" src="../model/fckeditor/fckeditor.js"></script>
<script language="javascript" src="../templates/<?php echo $moban;?>/js/common.js"></script>
<script language="javascript">
changeWebTitle('文章栏目管理');
function changetype(obj){
	var o=document.getElementById('link');
	var m=document.getElementById('moban');
	if(obj.value=='wailian'){
		o.style.display='';
	}else if(obj.value=='custom'){
		m.value="content.html";
		o.style.display='none';
	}else{
		m.value="list.html";
		o.style.display='none';
	}
}
</script>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>


<div class="ContentRight">
    <div class="ContentRightTop">
      <div class="BarLeft"></div>
      <div class="ContentRightTopText">文章栏目管理
	  <?php 
	  if($_GET['act']==''&&$_GET['catalog']==''){
	  	echo '<a href="?cmd='.$_REQUEST['cmd'].'&amp;act=add">（添加顶级栏目）</a>';
	  }else{
	  	echo "<a href='".$_SERVER['HTTP_REFERER']."'>（返回）</a>";
	  }
	  ?>
	  </div>
      <div class="BarRight"></div>
      <div class="Cal"></div>
    </div>
  <div class="ContentRightBox">
	<?php if($_REQUEST['act']==''){?>
<table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
		<?php 
		$id=$_GET['id']?$_GET['id']:0;
		$page=$_REQUEST['page']?($_REQUEST['page']==0?1:$_REQUEST['page']):1;
		$pagesize=20;
		$sqladd='where pid='.$id;
		$sql="select * from ".$prefix."column ".$sqladd." order by paixu limit ".($page-1)*$pagesize.",".$pagesize;
		$result=mysql_query($sql);
		function has_child($catid){
			global $prefix;
			
			$sql="select count(id) as child from ".$prefix."column where pid=".$catid;
			
			$result=mysql_query($sql);
			if($arr=mysql_fetch_array($result)){
				return $arr['child'];
			}
		}
		while($arr=mysql_fetch_array($result)){
		?>
		<tr>
          <td width="4%" align="center">
          <?php if(has_child($arr['id'])){echo "<a href='?cmd=column&id=".$arr['id']."'><img src='Images/listicon.gif'/></a>";}else{echo "<img src='Images/listicon.gif'/>";}?>
          </td>
          <td width="52%" height="25" align="left"><?php echo $arr['name']."[ID:".$arr['id']."]"?></td>
          <td width="13%" align="center">
          <?php
          if($arr['type']=='group'){
			  echo "栏目组"; 
		  }elseif($arr['type']=='article'){
			  echo "普通文章";
		  }elseif($arr['type']=='custom'){
			  echo "自定义页面";
		  }else{
			  echo "外链";
		  }
		  ?>
          </td>
          <td width="21%" align="center">
          <?php if($arr['type']=='group'){?>
          <a href="<?php echo "?cmd=".$_REQUEST['cmd']."&id=".$arr['id']."&act=add"?>">添加子类</a>&nbsp;
		  <?php	}elseif($arr['type']=='article'){?>
          <a href="<?php echo "?cmd=article&act=add&catalog=".$arr['id']?>">添加文章</a>&nbsp;
		  <?php	}elseif($arr['type']=='custom'){?>
          <a href="<?php echo "?cmd=column&act=custom&catalog=".$arr['id']?>">编辑内容</a>&nbsp;
		  <?php	}?>
          <a href="<?php echo "?cmd=".$_REQUEST['cmd']."&id=".$arr['id']."&act=modify"?>">修改</a>&nbsp;
          <a href="<?php echo "?cmd=".$_REQUEST['cmd']."&id=".$arr['id']."&act=del"?>" onclick="return confirm('您确认删除该条记录？并将删除其下的所有子类别，删除后将不可恢复！');">删除</a></td>
          <td width="10%" align="center"><?php echo $arr['paixu']?><?php if(!$arr['c_show']){echo "&nbsp;[隐藏]";}?></td>
        </tr>
        
		<?php
		}
		?>
        <tr>
          <td height="25" colspan="6" align="center">
		  <?php 
		  pagefy(get_list_count('column',$sqladd),$pagesize,'');
		  echo $pagenav;
		  ?>
          </td>
        </tr>
      </table>
    <?php }elseif($_REQUEST['act']=='custom'){?>
	  <form action="" name="customform" method="post">
      <input type="hidden" value="<?php echo $_SERVER['HTTP_REFERER'];?>"  name="fromurl"/>
    <table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
        <tr>
          <td width="22%" height="25" align="right">栏目名称：</td>
          <td width="78%">
          <?php echo $arr['name'];?>
          </td>
        </tr>
        <tr>
          <td height="25" align="right">自定义页面内容：</td>
          <td><script type="text/javascript">
			var sBasePath = '../model/fckeditor/' ;
			 
			var oFCKeditor = new FCKeditor( 'content' ) ;
			oFCKeditor.BasePath	= sBasePath ;
			oFCKeditor.Height	= 350 ;
			oFCKeditor.Width	= 580 ;
			oFCKeditor.ToolbarSet="Fengsh";
			oFCKeditor.Value	= "<?php echo str_replace('"','\"',preg_replace('/\r\n/','',$arr['content']));?>";
			oFCKeditor.Create() ;
		</script></td>
        </tr>
        <tr>
          <td height="25" align="right">&nbsp;</td>
          <td><input name="customSubmit" type="submit" id="customSubmit" value=" 提  交" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="Reset" type="reset" id="Reset" value=" 重 置 " />          </td>
        </tr>
      </table>
    </form>
    <?php }else{?>
	  <form action="" name="catalogform" method="post">
      <input type="hidden" value="<?php echo $_SERVER['HTTP_REFERER'];?>"  name="fromurl"/>
    <table width="100%" cellpadding="0" class="Table1" bordercolor="#4ab902" border="1" cellspacing="0">
        <tr>
          <td width="22%" height="25" align="right">栏目名称：</td>
          <td width="78%">
            <input name="name" type="text" id="name" value="<?php echo $arr['name'];?>" size="50"/>
            <span class="STYLE1">*</span>&nbsp;</td>
        </tr>
        <tr>
          <td height="25" align="right">是否显示：</td>
          <td>
              <input type="radio" name="c_show" id="c_show_0" value="1" checked>是
              <input type="radio" name="c_show" id="c_show_1" value="0" <?php if(!$arr['c_show']){echo "checked";}?>>否
            <span class="STYLE1">*</span>&nbsp;
         </td>
        </tr>
        <tr>
          <td height="25" align="right">栏目类型：</td>
          <td>
          <select name="type" onchange="changetype(this);">
          <option value="article" <?php if($arr['type']=='article'){echo "selected";}?>>普通文章</option>
          <option value="custom" <?php if($arr['type']=='custom'){echo "selected";}?>>自定义页面</option>
          <option value="group" <?php if($arr['type']=='group'){echo "selected";}?>>栏目组</option>
          <option value="wailian" <?php if($arr['type']=='wailian'){echo "selected";}?>>外链</option>
          </select>
          <input name="link" type="text" id="link" value="<?php echo $arr['link'];?>"<?php if($arr['type']!='wailian'){echo 'style="display:none;"';}?> >
          <span class="STYLE1">*</span>&nbsp; </td>
        </tr>
        <tr>
          <td height="25" align="right">上级栏目：</td>
          <td>
          <select name="pid" id="pid">
          <option value="0">无</option>
          <?php
		  get_catalog_list_by_id(0,0);
		  ?>
          </select>
          </td>
        </tr>
        <tr>
          <td height="25" align="right">上级目录：</td>
          <td>
          <?php 
		  if($_GET['id']&&$_GET['act']!='modify'){
			  $a=get_field_by_id('column','folder,id','id',$_REQUEST['id']);
		  }
		  if($a['folder']){
			  echo "<{FSpath}>/".$a['folder'];
		  }else{
			  echo "<{FSpath}>";
		  }
		  ?>
          </td>
        </tr>
        <tr>
          <td height="25" align="right">文件保存目录：</td>
          <td>
            <input name="folder" type="text" id="folder" size="50" value="<?php if($arr['name']!=''){echo $arr['folder'];}?>"/>
            <span class="STYLE1">*</span>&nbsp;</td>
        </tr>
        <tr>
          <td height="25" align="right">模板文件：</td>
          <td><input name="moban" type="text" id="moban" size="20" value="<?php if($arr['moban']!=''){echo $arr['moban'];}else{echo "list.html";}?>"/>
          <span class="STYLE1">*</span>&nbsp;一般无需修改</td>
        </tr>
        <tr>
          <td height="25" align="right">排序：</td>
          <td><input name="paixu" type="text" id="paixu" size="10" value="<?php echo $arr['paixu'];?>"></td>
        </tr>
        <tr>
          <td height="25" align="right">SEO标题：</td>
          <td><input name="title" type="text" id="title" size="50" value="<?php echo $arr['title'];?>"/></td>
        </tr>
        <tr>
          <td height="25" align="right">关键字：</td>
          <td><input name="keywords" type="text" id="keywords" size="50" value="<?php echo $arr['keywords'];?>"/></td>
        </tr>
        <tr>
          <td height="25" align="right">描述：</td>
          <td><input name="description" type="text" id="description" size="50" value="<?php echo $arr['description'];?>"/></td>
        </tr>
        <tr>
          <td height="25" align="right">&nbsp;</td>
          <td><input name="Submits" type="submit" id="Submits" value=" 提  交" />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="Reset" type="reset" id="Reset" value=" 重 置 " />          </td>
        </tr>
      </table>
    </form>
    <?php }?>
</div>
    <div class="Cal"></div>
</div>